package org.springblade.mes.logistics.entry.service;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.mes.logistics.entry.entity.EntryMaterial;
import org.springblade.mes.logistics.entry.mapper.EntryMaterialMapper;
import org.springblade.mes.logistics.entry.model.EntryMaterialBean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/** 
 * 服务
 * @Version 1.0
 */
@Slf4j
@Service
@DS("logistics")
public class EntryMaterialService extends BaseServiceImpl<EntryMaterialMapper, EntryMaterial> {
	
	@Transactional(rollbackFor = Exception.class)
	public boolean addData(EntryMaterialBean param) throws Exception{
		//保存
		EntryMaterial entity = param;
		return this.save(entity);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public boolean updateData(EntryMaterialBean param) throws Exception{
		//更新
		EntryMaterial newEntity = param;
		return this.updateById(newEntity);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public boolean deleteData(EntryMaterialBean param) throws Exception{
		if(param.getIdList() != null && param.getIdList().size() >0){
			UpdateWrapper<EntryMaterial> del = new UpdateWrapper<>();
			del.lambda().in(EntryMaterial::getId, param.getIdList());
			return this.remove(del);
		}
		if (param.getId() != null) {
            return this.removeById(param.getId());
        }
		return true;
	}

	
	public EntryMaterialBean findById(Long id) {
		EntryMaterialBean cond = new EntryMaterialBean();
		cond.setId(id);
		return findBySpec(cond);
    }

	
	public EntryMaterialBean findBySpec(EntryMaterialBean param) {
		List<EntryMaterialBean> list = this.baseMapper.queryList(param);
		if (list == null || list.size() == 0) {
			return null;
		}
		return list.get(0);
	}

	
	public List<EntryMaterialBean> findListBySpec(EntryMaterialBean param) {
		return this.baseMapper.queryList(param);
	}

	
	public IPage<EntryMaterialBean> findPageBySpec(IPage<EntryMaterialBean> page, EntryMaterialBean param) {
		List<EntryMaterialBean> dataList = baseMapper.queryPageList(page, param);
		return page.setRecords(dataList);
	}
}